﻿using Edu.Abstracts;
using Edu.Contracts;
using Edu.Pages;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace Edu.Web.Pages.Achievement
{
    public class CreateModalModel : EduPageModelBase
    {

        public const string UploadDir = "Achievement";

        private IAchievementAppService _appService;
        private IFileAppService _fileAppService;

        [BindProperty]
        public AchievementCreateUpdateDto CreateUpdateDto { get; set; }

        public CreateModalModel(IAchievementAppService appService, IFileAppService fileAppService)
        {
            _appService = appService;
            _fileAppService = fileAppService;
        }

        public void OnGet()
        {

        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (CreateUpdateDto.UploadActiveIcon != null)
            {
                var uploadResult = await _fileAppService.Upload(CreateUpdateDto.UploadActiveIcon, CreateModalModel.UploadDir);
                CreateUpdateDto.ActiveIcon = uploadResult.WebPath;
            }

            if (CreateUpdateDto.UploadDisableIcon != null)
            {
                var uploadResult = await _fileAppService.Upload(CreateUpdateDto.UploadDisableIcon, CreateModalModel.UploadDir);
                CreateUpdateDto.DisableIcon = uploadResult.WebPath;
            }

            await _appService.CreateAsync(CreateUpdateDto);
            return NoContent();
        }
    }
}